function sleep(numberMillis) {
    var now = new Date();
    var $voices="";
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
} 
wx.ready(function () {
	
  var flag=2;
  // 1 判断当前版本是否支持指定 JS 接口，支持批量判断
  document.querySelector('#checkJsApi').onclick = function () {
    wx.checkJsApi({
      jsApiList: [
        'getNetworkType',
        'previewImage'
      ],
      success: function (res) {
        alert(JSON.stringify(res));
      }
    });
  };
  // 2. 分享接口
 
  // 3 智能接口
  var voice = {
    localId: '',
    serverId: ''
  };
  // 3.1 识别音频并返回识别结果
  document.querySelector('#translateVoice').onclick = function () {
    if (voice.localId == '') {
      alert('请先使用 startRecord 接口录制一段声音');
      return;
    }
    wx.translateVoice({
      localId: voice.localId,
      complete: function (res) {
        if (res.hasOwnProperty('translateResult')) {
          alert('识别结果：' + res.translateResult);
        } else {
          alert('无法识别');
        }
      }
    });
  };
  // 4 音频接口
  // 4.2 开始录音
  document.querySelector('#startRecord').onclick = function () {
	  if(flag==2 || flag==1){
		  flag=0;
		  wx.startRecord({
		      cancel: function () {
		        alert('用户拒绝授权录音');
		      } 
		  });
	  }else{
		  flag=1;
		  wx.stopRecord({
		      success: function (res) {
		        voice.localId = res.localId;
		        server_id=voice.localId;
		      },
		      fail: function (res) {
		      }
		    }); 
	  }
  };
  // 4.3 停止录音
  document.querySelector('#stopRecord').onclick = function () {
    wx.stopRecord({
      success: function (res) {
        voice.localId = res.localId;
      },
      fail: function (res) {
      }
    });
  };
  // 4.4 监听录音自动停止
  wx.onVoiceRecordEnd({
    complete: function (res) {
      voice.localId = res.localId;
      alert('录音时间已超过一分钟');
    }
  });
  // 4.5 播放音频
  document.querySelector('#playVoice').onclick = function () {
    if (voice.localId == '') {
      alert('请先使用 startRecord 接口录制一段声音');
      return;
    }
    wx.playVoice({
      localId: voice.localId
    });
    $voices= $(this).find(".icon_711");
    $(".report_item .voice").find(".icon_711").remove("cur");
    if($voices.hasClass("cur")){
        $voices.removeClass("cur");
        audio.pause();
   }else{
        $voices.addClass("cur");
       audio.play();
    }
  };
  // 4.6 暂停播放音频
  document.querySelector('#pauseVoice').onclick = function () {
    wx.pauseVoice({
      localId: voice.localId
    });
  };
  // 4.7 停止播放音频
  document.querySelector('#stopVoice').onclick = function () {
    wx.stopVoice({
      localId: voice.localId
    });
  };
  // 4.8 监听录音播放停止
  wx.onVoicePlayEnd({
    complete: function (res) {
    	$voices.removeClass("cur");
    }
  });
  // 4.8 上传语音
  document.querySelector('#uploadVoice').onclick = function () {
	  
	if(type==1){
		wx.uploadVoice({
		      localId: voice.localId,
		      success: function (res) {
		        voice.serverId = res.serverId;
		        server_id=res.serverId;
		        var question="";
		    	if(type=="1"){
		    		if(""==server_id){
		    			alert("请输入语音");
		    			return false;
		    		}
		    		question=server_id;
		    	}else if(type=="0"){
		    		if(""==$("#txt_question_content").val()){
		    			alert("请输入您要询问的问题");
		    			return false;
		    		}
		    		question=$("#txt_question_content").val();
		    	}else{
		    		alert("请输入问题或语音");
		    		return false;
		    	}
		    	if(temp==0){
		    		alert("请输入问题或语音");
		    		return false;
		    	}
		    	location.href="AskHuifu?type="+type+"&question="+question+"&askId="+askId;
		      }
		    });
	 } else{
		 var question="";
	    	if(type=="1"){
	    		if(""==server_id){
	    			alert("请输入语音");
	    			return false;
	    		}
	    		question=server_id;
	    	}else if(type=="0"){
	    		if(""==$("#txt_question_content").val()){
	    			alert("请输入您要询问的问题");
	    			return false;
	    		}
	    		question=$("#txt_question_content").val();
	    	}else{
	    		alert("请输入问题或语音");
	    		return false;
	    	}
	    	if(temp==0){
	    		alert("请输入问题或语音");
	    		return false;
	    	}
	    	location.href="AskHuifu?type="+type+"&question="+question+"&askId="+askId;
	 } 
	  
    
  };
  // 4.9 下载语音
  document.querySelector('#downloadVoice').onclick = function () {
    if (voice.serverId == '') {
      alert('请先使用 uploadVoice 上传声音');
      return;
    }
    wx.downloadVoice({
      serverId: voice.serverId,
      success: function (res) {
        alert('下载语音成功，localId 为' + res.localId);
        voice.localId = res.localId;
      }
    });
  };
  wx.onMenuShareAppMessage(shareData);
  wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
});